#region Usings
using System;
using System.Diagnostics;
#endregion

namespace BK.Model
{
    public static class TreeNodeEntityExtensions
    {
        public static string CalculateFullName(this ITreeNodeEntity treeNodeEntity)
        {
            var result = string.Empty;
            IterateToRoot(treeNodeEntity, delegate(ITreeNodeEntity entity)
                {
                    result = result == string.Empty ? entity.Name : "\\" + entity.Name;
                });

            return result;
        }

        public static int CalculateNestingLevel(this ITreeNodeEntity treeNodeEntity)
        {
            var result = -1;
            IterateToRoot(treeNodeEntity, delegate { result++; });

            return result;
        }

        private static void IterateToRoot(ITreeNodeEntity treeNodeEntity, Action<ITreeNodeEntity> action)
        {
            ITreeNodeEntity current = treeNodeEntity;
            while (current != null)
            {
                action(current);
                current = current.Parent;
            }
        }
    }
}